package com.tien.action.core;

import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Controller;

import com.tien.model.Menu;
import com.tien.model.User;
import com.tien.service.MenuService;
import com.tien.utils.AbstractAction;
import com.tien.utils.CallBackJSONUtil;
import com.tien.utils.SecurityUserUtil;

/**
 * @author tienxin
 * @E-mail g.tianxin@gmail.com
 * @version 2012-2-19 上午02:05:04
 */
@Controller
@Scope("prototype")
public class MenuManagerAction extends AbstractAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2243852966920525623L;
	private static final Log logger = LogFactory
			.getLog(MenuManagerAction.class);
	@Autowired
	private MenuService menuService;

	private String menuJson;

	private Menu menu;

	private Long parentid;

	public String execute() {
		List<Menu> menus = null;
		try {
			Integer menuGroupId = SecurityUserUtil.getCurrentUser().getMenu_group_id();
//			menus = menuService.findAllMenus(0l);
			menus = menuService.findAllMenus(Long.parseLong(menuGroupId.toString()));
			// menuJson = MenuTree.parseMenuJason(menus);
			JSONArray menuJsonArray = JSONArray.fromObject(menus);
			menuJson = menuJsonArray.toString();
			logger.info("Load menu json successfully! menuJson:" + menuJson);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return "success";
	}

	public String updateMenuItem() {
		try {
			menuService.updateMenu(menu);
			logger.info("Menu updated, Id :" + menu.getId());
			CallBackJSONUtil.printSuccessState(null, response);
		} catch (AccessDeniedException e) {
			logger.error(e.getMessage());
			CallBackJSONUtil.printAccessDeniedState(e, response);// AccessDeniedException
		} catch (Exception e) {
			logger.error(e.getMessage());
			CallBackJSONUtil.printErrorState(e, response);// AccessDeniedException
		}
		return null;
	}

	public String addMenuItem() {
		try {
			menu.setpId(parentid);
			menuService.addMenu(menu);
			logger.info("Menu added, Id :" + menu.getId());
			CallBackJSONUtil.printSuccessState(null, response);
		}catch (AccessDeniedException e) {
			logger.error(e.getMessage());
			CallBackJSONUtil.printAccessDeniedState(e, response);// AccessDeniedException
		} catch (Exception e) {
			logger.error(e.getMessage());
			CallBackJSONUtil.printErrorState(e, response);// AuthenticationCredentialsNotFoundException
		}
		return null;
	}

	public String findMenuById() {
		try {
			Menu menuDetail = menuService.findMenuById(menu.getId());
			logger.info("Menu find by Id :" + menuDetail.getId());
			CallBackJSONUtil.printSuccessState(menuDetail, response);
		} catch (Exception e) {
			logger.error(e.getMessage());
			CallBackJSONUtil.printErrorState(e, response);
		}
		return null;
	}

	public String deleteMenuById() {
		try {
			menuService.deleteMenuById(menu.getId());
			logger.info("Menu find by Id :" + menu.getId());
			CallBackJSONUtil.printSuccessState(null, response);
		} catch (AccessDeniedException e) {
			logger.error(e.getMessage());
			CallBackJSONUtil.printAccessDeniedState(e, response);// AccessDeniedException
		}catch (Exception e) {
			logger.error(e.getMessage());
			CallBackJSONUtil.printErrorState(e, response);
		}
		return null;
	}

	public String getMenuJson() {
		return menuJson;
	}

	public void setMenuJson(String menuJson) {
		this.menuJson = menuJson;
	}

	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	public Long getParentid() {
		return parentid;
	}

	public void setParentid(Long parentid) {
		this.parentid = parentid;
	}

}
